//
//  AddressSelectedToolsView.m
//  CRSCMOA
//
//  Created by c on 16/8/9.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "AddressSelectedToolsView.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight 30
#import "JNTAddressBookViewController.h"

@interface AddressSelectedToolsView ()

@property (nonatomic, assign)NSInteger btnCount;
@property (nonatomic, strong) UITextField *textField;

@end

@implementation AddressSelectedToolsView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initLHSelectedNavigationViewToolsWithFrame:(CGRect)frame ButtonTitle:(NSArray<NSString *> *)btnTitleArr searchView:(UIView *)searchView {
    
    self.btnCount = btnTitleArr.count;
    frame = CGRectMake(frame.origin.x, frame.origin.y, kWidth, frame.size.height);
    self = [super initWithFrame:frame];
    if (self) {
        
        UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [myBtn setFrame:CGRectMake(frame.size.width-100, 5, 100, 30)];
        [myBtn setTitle:@"我的单位" forState:UIControlStateNormal];
        [myBtn addTarget:self action:@selector(myAddressBtn:) forControlEvents:UIControlEventTouchUpInside];
        myBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        myBtn.backgroundColor = [UIColor blueColor];
        [self addSubview:myBtn];

        [self addSubview:searchView];
        
        for (int i = 0; i < btnTitleArr.count; i++) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            btn.tintColor = [UIColor blackColor];
            [btn setTitle:btnTitleArr[i] forState:UIControlStateNormal];
            if (i == 3) {
                
                btn.frame = CGRectMake(kWidth*i/(btnTitleArr.count), 50, kWidth/btnTitleArr.count+40, kHeight-2);
            }else {
                
                btn.frame = CGRectMake((kWidth)*i/(btnTitleArr.count), 50, kWidth/btnTitleArr.count, kHeight-2);
            }
            btn.tag = 1000+i;
//            btn.titleLabel.adjustsFontSizeToFitWidth = YES;
            [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            
            UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnImage setImage:[UIImage imageNamed:@"ic_arrow.png"] forState:UIControlStateNormal];
            [btnImage setFrame:CGRectMake(btn.frame.size.width-10, 0, 10, kHeight-2)];
            btnImage.tag = 2000+i;
            [btn addSubview:btnImage];
            [self addSubview:btn];
            
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0 , frame.size.height-1, frame.size.width, 1)];
            label.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
            [self addSubview:label];
        }
        
        
    }
    return self;
}

- (void)btnAction:(UIButton *)sender {

    UIButton *btnImage = [self viewWithTag:2000];
    UIButton *btn1 = [self viewWithTag:1001];
    UIButton *btnImage1 = [self viewWithTag:2001];
    UIButton *btn2 = [self viewWithTag:1002];
    UIButton *btnImage2 = [self viewWithTag:2002];
    UIButton *btn3 = [self viewWithTag:1003];
    UIButton *btnImage3 = [self viewWithTag:2003];
    
    if (self.selectedPageBlock) {
        
        self.selectedPageBlock(sender.tag-1000);
    }
        switch (sender.tag) {
            case 1000:
                btnImage.hidden = YES;
                btn1.hidden = YES;
                btnImage1.hidden = YES;
                btn2.hidden = YES;
                btnImage2.hidden = YES;
                btn3.hidden = YES;
                btnImage3.hidden = YES;
                break;
            case 1001:
                btnImage1.hidden = YES;
                btn2.hidden = YES;
                btnImage2.hidden = YES;
                btn3.hidden = YES;
                btnImage3.hidden = YES;
                break;
            case 1002:
                btnImage2.hidden = YES;
                btn3.hidden = YES;
                btnImage3.hidden = YES;
                break;
            default:
                break;
        }
    self.isOpen = NO;
    
}

- (void)myAddressBtn: (UIButton *)sender {
    
//    JNTAddressBookViewController *jntVC = [[JNTAddressBookViewController alloc] init];
//    [jntVC makeData];
//    [jntVC.tableView reloadData];
    
    if ([self.delegate respondsToSelector:@selector(buttonAction:)]) {
        
        [self.delegate buttonAction:sender];
    }
    
    self.isOpen = !self.isOpen;
    UIButton *btn = [self viewWithTag:1000];
    UIButton *btnImage = [self viewWithTag:2000];
    UIButton *btn1 = [self viewWithTag:1001];
    UIButton *btnImage1 = [self viewWithTag:2001];
    UIButton *btn2 = [self viewWithTag:1002];
    UIButton *btnImage2 = [self viewWithTag:2002];
    UIButton *btn3 = [self viewWithTag:1003];
    UIButton *btnImage3 = [self viewWithTag:2003];

    btn.hidden = NO;
    btnImage.hidden = NO;
    btn1.hidden = NO;
    btnImage1.hidden = NO;
    btn2.hidden = NO;
    btnImage2.hidden = NO;
    btn3.hidden = NO;
    btnImage3.hidden = NO;
    NSLog(@"33333");
}

@end
